/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package windowComponents;

import busEvents.ChangeViewEvent;
import busEvents.ShowContact;
import com.adamtaft.eb.EventBusService;
import data.contact.Contact;
import enums.ViewEnum;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Observable;
import java.util.Observer;
import javax.swing.Action;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;
import org.apache.commons.lang3.StringUtils;
import javax.mail.Message.RecipientType;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

/**
 *
 * @author Arcanis
 */
public class EmailLabel extends JLabel implements Observer {

    private Contact contact;
    private JPopupMenu menu;
    private RecipientType type = null;
    private boolean from = false;

    public EmailLabel(final Contact contact, RecipientType type) {
        this(contact);
        this.type = type;
        updateLabel();
    }
    
    public EmailLabel(final Contact contact, boolean from) {
        this(contact);
        this.from = from;
        updateLabel();
    }

    public EmailLabel(final Contact contact) {
        super();
//        this.setFont(new Font("Arial", Font.PLAIN, 15));        
        this.contact = contact;
        contact.addObserver(this);
        updateLabel();
        setBorder(new LineBorder(Color.black, 1, false));

        setCursor(new Cursor(Cursor.HAND_CURSOR));

        menu = new JPopupMenu("Pokusik");

        JMenuItem email = new JMenuItem(contact.getAddress().getAddress());
        email.setEnabled(false);
        menu.add(email);
        JMenuItem jedna = new JMenuItem("Zobrazit kontakt");
        jedna.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Icon-Disk01-Blue.png")));
        jedna.setMnemonic('k');
        menu.add(jedna);        
        
        this.add(menu);

        this.setToolTipText(contact.getAddress().getAddress());

        addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                menu.show(e.getComponent(), e.getX(), e.getY());
            }
        });

        jedna.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                EventBusService.publish(new ShowContact(contact));
                EventBusService.publish(new ChangeViewEvent(ViewEnum.CONTACTS));
            }
        });
    }

    private void updateLabel() {
        String text = "<html>&nbsp;";

        if (type != null) {
            text += type.toString() + ": ";
        }
        
        if (from) {
            text += "From: ";
        }

        if (StringUtils.isEmpty(contact.getName()) && StringUtils.isEmpty(contact.getSurname())) {
            text += "<b>" + contact.getAddress().getAddress() + "</b>";
        } else {
            text += "<b>" + (contact.getName() + " " + contact.getSurname()).trim() + "</b>";
        }

        text += "&nbsp;</html>";
        setText(text);

    }

    @Override
    public void update(Observable o, Object arg) {
        if (o == contact) {
            updateLabel();
        }
    }
}
